package com.sharesrc.jdev.wiimotedemo.views;

import java.awt.Container;
import java.awt.GridBagConstraints;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.wiigee.event.AccelerationEvent;
import org.wiigee.logic.Gesture;

import com.sharesrc.jdev.wiimotedemo.common.constant.Constants;
import com.sharesrc.jdev.wiimotedemo.common.constant.Themes;
import com.sharesrc.jdev.wiimotedemo.common.util.UIUtils;

public class GestureInfoPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 201308170113L;

	public JLabel lblMinAcceleration, lblMaxAcceleration, lblCountOfData;
	public JTextField tfMinAcceleration, tfMaxAcceleration, tfCountOfData;

	Gesture gesture;

	public GestureInfoPanel() {
		super();
	}

	public GestureInfoPanel(Gesture gesture) {
		this();
		this.gesture = gesture;
		initUIComponents();
	}

	private void initUIComponents() {
		this.lblMinAcceleration = new JLabel(Constants.Texts.MIN_ACCELERATION);
		this.lblMaxAcceleration = new JLabel(Constants.Texts.MAX_ACCELERATION);
		this.lblCountOfData = new JLabel(Constants.Texts.COUNT_OF_DATA);
		this.tfMinAcceleration = new JTextField(String.valueOf(this.gesture.getMinAcceleration()));
		this.tfMaxAcceleration = new JTextField(String.valueOf(this.gesture.getMaxAcceleration()));
		this.tfCountOfData = new JTextField(String.valueOf(this.gesture.getCountOfData()));

		addLine(this.lblMinAcceleration, this.tfMinAcceleration);
		addLine(this.lblMaxAcceleration, this.tfMaxAcceleration);
		addLine(this.lblCountOfData, this.tfCountOfData);
		for (int i = 0; i < this.gesture.getCountOfData(); i++) {
			AccelerationEvent acelerationEventData = this.gesture.getData().get(i);
			String acelerationEventDataStr = "x = " + acelerationEventData.getX() + ", y = " + acelerationEventData.getY() + ", z = " + acelerationEventData.getZ() + ", absValue = " + acelerationEventData.getAbsValue();
			addLine(new JLabel("     " + Constants.Texts.DATA + "-" + i), new JTextField(acelerationEventDataStr));
		}

		setBorder(BorderFactory.createMatteBorder(1, 1, 0, 1, Themes.Colore.DETAILED_INFO_PANEL_BORDER));
	}

	private void addLine(JLabel lbl, JTextField tf) {
		addLine(this, lbl, tf);
	}

	private void addLine(Container container, JLabel lbl, JTextField tf) {
		lbl.setOpaque(true);
		lbl.setBackground(Themes.Colore.DETAILED_INFO_PANEL_HEADLINE_BG);
		lbl.setForeground(Themes.Colore.DETAILED_INFO_PANEL_HEADLINE_FG);
		lbl.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Themes.Colore.DETAILED_INFO_PANEL_BORDER), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
		tf.setOpaque(true);
		tf.setEditable(false);
		tf.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Themes.Colore.DETAILED_INFO_PANEL_BORDER), BorderFactory.createEmptyBorder(5, 15, 5, 5)));

		UIUtils.addComponent(container, lbl, 1, 1, 1, 1, GridBagConstraints.BOTH, 0, 0, 0, 0, GridBagConstraints.LINE_START);
		UIUtils.addComponent(container, tf, GridBagConstraints.REMAINDER, 1, 5, 1, GridBagConstraints.BOTH, 0, 0, 0, 0, GridBagConstraints.CENTER);
	}
}
